package com.code2roc.jarmanage.socket;

import com.code2roc.jarmanage.util.LogUtil;
import com.code2roc.jarmanage.util.SocketUtil;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint("/websocket/ssh")
public class SSHWebSocketServer {
    private static SocketUtil socketUtil = new SocketUtil();

    @OnOpen
    public void onOpen(Session session) {
        socketUtil.initConnection(session);
        System.out.println("新连接已打开：" + session.getId());
    }

    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("收到消息：" + message);
        try {
            socketUtil.recvHandle(message, session);
        } catch (Exception e) {
            LogUtil.error(e);
        }
    }

    @OnClose
    public void onClose(Session session) {
        socketUtil.close(session);
        System.out.println("连接已关闭：" + session.getId());
    }
}
